﻿using UnityEngine;
using System.Collections;

public class TruckEvent : MonoBehaviour {

	GameObject EventTimer;
	GameObject ShopBtn;

    GameObject TruckObj;

	float fSpeed;
	public bool bParking = false;
	bool bParkingOnce = true;

    public TRUCK_EVENT EventID;

    public bool m_bSetTimerOnce;

	void Start(){
        if (Clock.iHour >= 1 && Clock.iHour <= 14)
        {
            GetComponent<UISprite>().color = new Color(29/255f, 26/255f, 24/255f);
        }
        else
        {
            GetComponent<UISprite>().color = new Color(189 / 255f, 173 / 255f, 160 / 255f);
        }

		switch((int)EventID)
		{
		case 0:
			GetComponent<UISprite> ().spriteName = "event0";
            GameObject.Find("DataLogic").GetComponent<DataManager>().ResetHireList();
			break;
		case 1:
			GetComponent<UISprite> ().spriteName = "event1";
			break;
		case 2:
			GetComponent<UISprite> ().spriteName = "event2";
			break;
		case 3:
			GetComponent<UISprite> ().spriteName = "event3";
			break;
		}
		transform.localScale = new Vector3 (0.15f, 0.1f);
		//GetComponent<UISprite> ().MakePixelPerfect();
	}
	// Update is called once per frame
	void Update () {
        fSpeed = Clock.fElapseSpeed * 1.225f;

		if (transform.localPosition.x < 0.01f && bParkingOnce) {
			bParking = true;
			bParkingOnce = false;
            
			//StartCoroutine(ParkingTimer(15.0f)); // 주차시간 // 타이머에 주차시간 따로만들어둠

            TruckObj = Instantiate(Resources.Load("Prefaps/Props/SupplyTruckObj") as GameObject) as GameObject;
            TruckObj.GetComponent<SupplyTruckObj>().m_EventId = this.EventID;
            TruckObj.transform.parent = GameObject.Find("StaticObj").transform;

            switch (EventID)
            {
                case TRUCK_EVENT.AMMO:
                    ResourceMgr.GetInstance().CreateSystemMsg(string.Format(xmlLoader.GetInstance().TextLoad(XML_INDEX.SYSTEM_MSG, "TruckArrive"), "Ammo"));
                    break;

                case TRUCK_EVENT.REINFORCE:
                    ResourceMgr.GetInstance().CreateSystemMsg(string.Format(xmlLoader.GetInstance().TextLoad(XML_INDEX.SYSTEM_MSG, "TruckArrive"), "Reinforce"));
                    break;

                case TRUCK_EVENT.SUPPLY:
                    ResourceMgr.GetInstance().CreateSystemMsg(string.Format(xmlLoader.GetInstance().TextLoad(XML_INDEX.SYSTEM_MSG, "TruckArrive"), "Supply"));
                    break;
            }
            
		}
        
		if (bParking) {
            GetComponent<UISprite>().color = GameObject.Find("Timer").GetComponent<UISprite>().color;
		} else {
			transform.localPosition -= new Vector3 (fSpeed, 0);
			GetComponent<TweenPosition> ().from.x = transform.localPosition.x;
			GetComponent<TweenPosition> ().to.x = transform.localPosition.x;
		}

		if(transform.localPosition.x <= -424){
			DestroyThisTruck();
            if (!m_bSetTimerOnce)
            {
                GameObject.Find("EventMgr").GetComponent<EventMgr>().SetTruckEventTimer(); // 이벤트 타이머 재시작
                m_bSetTimerOnce = true;
            }
            
		}
	}


    public void DestroyThisTruck()
    {
        EventDelegate.Add(GetComponent<TweenAlpha>().onFinished, DeleteThis);
        GetComponent<TweenAlpha>().Play(false);
    }

	void DeleteThis(){
		Destroy (gameObject);
	}

    //IEnumerator ParkingTimer(float fTime)
    //{
    //    yield return new WaitForSeconds(fTime);
    //    bParking = false;
    //}
}
